﻿Imports MetodosComuns
Imports extranet.BC
Imports extranet.BE
Imports System.Collections
Imports System.Collections.Generic

Partial Class Eventos_AdmEvtGerenciamentoInscrito
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '==================       PROCEDIMENTO  PADRÃO  SEGURANÇA     =============================
        Dim objBESeguranca As BESeguranca
        Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
        If IsNothing(objBEUsuario) Then Response.Redirect(TELA_LOGOUT)
        Session("strTela") = MetodosComuns.GetCurrentPageName()
        If Not IsPostBack Then          ' SEGURANÇA
            objBESeguranca = BCSeguranca.SelecionaPermissoes(objBEUsuario.GrupoAcesso, Session("strTela"))
            If Not objBESeguranca.Autorizado Then Response.Redirect(TELA_LOGOUT)
            Session("objBESeguranca") = objBESeguranca
        Else
            objBESeguranca = Session("objBESeguranca")
        End If

        If Not IsPostBack Then
            Dim strIdEvento As String = Request.QueryString("Evento")
            Dim objBEEvento As BEEvento
            lblTitulo.Text = "Inscritos no Evento"
            lblCaptionId.Visible = True
            lblId.Visible = True
            lblId.Text = strIdEvento
            objBEEvento = BCEvento.SelecionarEvento(strIdEvento)
            lblDtFim.Text = objBEEvento.DtFim.ToString("dd/MM/yyyy")
            lblDtInicio.Text = objBEEvento.DtInicio.ToString("dd/MM/yyyy")
            lblNome.Text = objBEEvento.Nome
            chbAtivo.Checked = IIf(objBEEvento.StatusEvento = "Ativo", True, False)
            grvInscritos.DataSource = objBEEvento.InscritosEvento
            grvInscritos.DataBind()
            panListaInscritos.Visible = True
            panDetalhesInscrito.Visible = False
        End If
    End Sub

    Protected Sub grvInscritos_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grvInscritos.RowEditing
        Dim objBEInscrito As BEInscrito = BCInscrito.SelecionarInscrito(grvInscritos.DataKeys(e.NewEditIndex).Value.ToString())
        Dim lstBEInscrito As New List(Of BEInscrito)
        If objBEInscrito.Aprovado = True Then
            btnReprovarInscrito.Visible = True
            btnAprovarInscrito.Visible = False
        Else
            btnReprovarInscrito.Visible = False
            btnAprovarInscrito.Visible = True
        End If
        panDetalhesInscrito.Visible = True
        panListaInscritos.Visible = False
        lstBEInscrito.Add(objBEInscrito)
        dtvDetalhesInscrito.DataSource = lstBEInscrito
        dtvDetalhesInscrito.DataBind()
        dtvDetalhesInscrito.PageIndex = 0
    End Sub

    Protected Sub btnAprovarInscrito_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAprovarInscrito.Click
        Dim objBEInscrito As BEInscrito = BCInscrito.AprovarInscrito(dtvDetalhesInscrito.DataKey(0).ToString())
        Dim lstBEInscrito As New List(Of BEInscrito)
        btnReprovarInscrito.Visible = True
        btnAprovarInscrito.Visible = False
        lstBEInscrito.Add(objBEInscrito)
        dtvDetalhesInscrito.DataSource = lstBEInscrito
        dtvDetalhesInscrito.DataBind()
        dtvDetalhesInscrito.PageIndex = 0
        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
        System.Web.HttpContext.Current.Response.Write("alert(""Inscrito Aprovado!"")" & vbCrLf)
        System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
    End Sub

    Protected Sub btnReprovarInscrito_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReprovarInscrito.Click
        Dim objBEInscrito As BEInscrito = BCInscrito.ReprovarInscrito(dtvDetalhesInscrito.DataKey(0).ToString())
        Dim lstBEInscrito As New List(Of BEInscrito)
        btnReprovarInscrito.Visible = True
        btnAprovarInscrito.Visible = False
        lstBEInscrito.Add(objBEInscrito)
        dtvDetalhesInscrito.DataSource = lstBEInscrito
        dtvDetalhesInscrito.DataBind()
        dtvDetalhesInscrito.PageIndex = 0
        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
        System.Web.HttpContext.Current.Response.Write("alert(""Inscrito Reprovado!"")" & vbCrLf)
        System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click
        Dim objBEEvento As BEEvento = BCEvento.SelecionarEvento(lblId.Text)
        panDetalhesInscrito.Visible = False
        panListaInscritos.Visible = True
        grvInscritos.DataSource = objBEEvento.InscritosEvento
        grvInscritos.DataBind()
    End Sub

    Protected Sub chbEmailHeader_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chbEmailHead = DirectCast(sender, CheckBox)
        For Each objGRRow As GridViewRow In grvInscritos.Rows
            Dim chb As CheckBox = objGRRow.FindControl("chbEmail")
            chb.Checked = chbEmailHead.Checked
        Next
    End Sub

    Protected Sub chbAprovadosHeader_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chbAprovadoHead = DirectCast(sender, CheckBox)
        For Each objGRRow As GridViewRow In grvInscritos.Rows
            Dim chb As CheckBox = objGRRow.FindControl("chbAprovado")
            chb.Checked = chbAprovadoHead.Checked
        Next
    End Sub

    Protected Sub btnEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmail.Click
        Dim lstInscritosEmail As New List(Of String)
        For i As Integer = 0 To grvInscritos.Rows.Count - 1
            Dim chb As CheckBox = grvInscritos.Rows(i).FindControl("chbEmail")
            If (chb.Checked) Then
                lstInscritosEmail.Add(grvInscritos.DataKeys(i).Value.ToString())
            End If
        Next
        If lstInscritosEmail.Count > 0 Then
            Session("lstInscritosEmail") = lstInscritosEmail
            Response.Redirect("AdmEvtEnviarEmail.aspx?Evento=" & lblId.Text)
        End If
    End Sub

    Protected Sub btnSalvar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
        Dim lstInscritos As New List(Of BEInscrito)
        For i As Integer = 0 To grvInscritos.Rows.Count - 1
            Dim chb As CheckBox = grvInscritos.Rows(i).FindControl("chbAprovado")
            Dim objBEInscrito As BEInscrito = BCInscrito.SelecionarInscrito(grvInscritos.DataKeys(i).Value.ToString())
            If objBEInscrito.Aprovado <> chb.Checked Then
                objBEInscrito.Aprovado = chb.Checked
                lstInscritos.Add(objBEInscrito)
            End If
        Next
        For Each objBEInscrito As BEInscrito In lstInscritos
            If objBEInscrito.Aprovado Then
                BCInscrito.AprovarInscrito(objBEInscrito.IdInscrito)
            Else
                BCInscrito.ReprovarInscrito(objBEInscrito.IdInscrito)
            End If
        Next

    End Sub
End Class
